package book.test01;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

public class SalaryReduce extends Reducer<Text, Text, Text, Text> {
    private Text v = new Text();

    @Override
    protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
        //计算每一个城市的数据数
        double count = 0;
        double sumMinSalary = 0;
        double sumMaxSalary = 0;
        //计算平均工资最小值
        double avgMinSalary;
        //计算平均工资最小值
        double avgMaxSalary;
        for (Text value : values) {
            String[] salary = value.toString().split(",");
            //计算总值
            sumMinSalary += Integer.parseInt(salary[0]);
            sumMaxSalary += Integer.parseInt(salary[1]);
            count++;
        }
        //计算平均值
        avgMinSalary = sumMinSalary / count;
        avgMaxSalary = sumMaxSalary / count;
        v.set(avgMinSalary + "\t" + avgMaxSalary);
        context.write(key, v);
    }
}
